<template>
	<div class="sys-exlog-container" v-loading="state.loading">
		<el-card shadow="hover" :body-style="{ paddingBottom: '0' }">
			<el-form :model="state.queryParams" ref="queryForm" :inline="true">
				<el-form-item label="开始时间">
					<el-date-picker v-model="state.queryParams.beginTime" type="datetime" placeholder="开始时间" value-format="YYYY-MM-DD HH:mm:ss" :shortcuts="shortcuts" />
				</el-form-item>
				<el-form-item label="结束时间">
					<el-date-picker v-model="state.queryParams.endTime" type="datetime" placeholder="结束时间" value-format="YYYY-MM-DD HH:mm:ss" :shortcuts="shortcuts" />
				</el-form-item>

				<el-form-item label="方法名称">
					<el-input v-model="state.queryParams.methodName" placeholder="方法名称" clearable />
				</el-form-item>
				
				<el-form-item label="类名">
					<el-input v-model="state.queryParams.className" placeholder="类名" clearable />
				</el-form-item>
				<el-form-item label="操作人">
					<el-input v-model="state.queryParams.account" placeholder="操作人" clearable />
				</el-form-item>

				<el-form-item>
					<el-button-group>
						<el-button type="primary" icon="ele-Search" @click="handleQuery" v-auth="'logex:searchBtn'"> 查询 </el-button>
						<el-button icon="ele-Refresh" @click="resetQuery"> 重置 </el-button>
					</el-button-group>
				</el-form-item>
				<el-form-item>
					<el-button icon="ele-DeleteFilled" type="danger" @click="clearLog" v-auth="'logex:clearBtn'"> 清空 </el-button>
					<el-button icon="ele-FolderOpened" @click="exportLog" v-auth="'logex:exportBtn'" disabled> 导出 </el-button>
				</el-form-item>
			</el-form>
		</el-card>

		<el-card class="full-table" shadow="hover" style="margin-top: 5px">
			<el-table :data="state.logData" @sort-change="sortChange" style="width: 100%" border :row-class-name="tableRowClassName">
				<el-table-column type="index" label="序号" width="55" align="center" />
				<el-table-column prop="methodName" label="方法名" width="180" header-align="center" show-overflow-tooltip />
				<el-table-column prop="className" label="类名" width="220" header-align="center" show-overflow-tooltip />
				<el-table-column prop="exceptionMsg" label="异常信息" header-align="center" show-overflow-tooltip />
				<el-table-column prop="exceptionSource" label="异常源" header-align="center" show-overflow-tooltip />
				<el-table-column prop="stackTrace" label="堆栈信息" header-align="center" show-overflow-tooltip />
				<el-table-column prop="paramsObj" label="参数对象" header-align="center" show-overflow-tooltip />
				
				<el-table-column prop="exceptionTime" label="异常时间" width="160" align="center" fixed="right" show-overflow-tooltip />
				<el-table-column prop="name" label="操作人" width="120" align="center" fixed="right" show-overflow-tooltip />
				<el-table-column label="操作" width="80" align="center" fixed="right" show-overflow-tooltip>
					<template #default="scope">
						<el-button icon="ele-InfoFilled" size="small" text type="primary" @click="viewDetail(scope.row)" v-auth="'logex:detail'">详情 </el-button>
					</template>
				</el-table-column>
				
			</el-table>
			<el-pagination
				v-model:currentPage="state.tableParams.pageNo"
				v-model:page-size="state.tableParams.pageSize"
				:total="state.tableParams.total"
				:page-sizes="[10, 20, 50, 100]"
				size="small"
				background
				@size-change="handleSizeChange"
				@current-change="handleCurrentChange"
				layout="total, sizes, prev, pager, next, jumper"
			/>
		</el-card>
		<el-dialog v-model="state.dialogVisible" draggable fullscreen>
			<template #header>
				<div style="color: #fff">
					<el-icon size="16" style="margin-right: 3px; display: inline; vertical-align: middle"> <ele-Document /> </el-icon>
					<span> 日志详情 </span>
				</div>
			</template>
			<pre v-loading="state.loadingDetail">{{ state.content }}</pre>
		</el-dialog>
	</div>
</template>

<script lang="ts" setup name="sysExLog">
import { onMounted, reactive } from 'vue';
import { ElMessage } from 'element-plus';
import { downloadByData, getFileName } from '/@/utils/download';
import { formatDate } from '/@/utils/formatTime'; 

import { apiSysLogExPagePost,apiSysLogExClearPost,apiSysLogExExportPost,apiSysLogExDetailIdGet } from '/@/api-services/api';

const state = reactive({
	loading: false,
	loadingDetail: false,
	tenantList: [] as Array<any>,
	queryParams: {
		beginTime: undefined,
		endTime: undefined,
		name: undefined,
		methodName: undefined,
		className: undefined,
		account: undefined,
	},
	tableParams: {
		pageNo: 1,
		pageSize: 50,
		field: 'createTime', // 默认的排序字段
		order: 'descending', // 排序方向
		descStr: 'descending', // 降序排序的关键字符
		total: 0 as any,
	},
	logData: [] as Array<SysLogEx>,
	dialogVisible: false,
	content: '',
});

onMounted(async () => {
	handleQuery();
});

// 查询操作
const handleQuery = async () => {
	if (state.queryParams.beginTime == null) state.queryParams.beginTime = undefined;
	if (state.queryParams.endTime == null) state.queryParams.endTime = undefined; 
	if (state.queryParams.name == null) state.queryParams.name = undefined;
	if (state.queryParams.methodName == null) state.queryParams.methodName = undefined;
	if (state.queryParams.className == null) state.queryParams.className = undefined; 
	if (state.queryParams.account == null) state.queryParams.account = undefined; 

	state.loading = true;
	let params = Object.assign(state.queryParams, state.tableParams);
	var res = await apiSysLogExPagePost(params);
	state.logData = res.data.data?.rows ?? [];
	state.tableParams.total = res.data.data?.totalRows;
	state.loading = false;
};

// 重置操作
const resetQuery = () => {
	state.queryParams.beginTime = undefined;
	state.queryParams.endTime = undefined; 
	state.queryParams.name = undefined;
	state.queryParams.methodName = undefined;
	state.queryParams.className = undefined; 
	state.queryParams.account = undefined; 
	handleQuery();
};

// 清空日志
const clearLog = async () => {
	state.loading = true;
	await apiSysLogExClearPost();
	state.loading = false;

	ElMessage.success('清空成功');
	handleQuery();
};

// 导出日志
const exportLog = async () => {
	state.loading = true;
	var res = await  apiSysLogExExportPost(state.queryParams, { responseType: 'blob' });
	console.log('导出日志',res)
	state.loading = false;

	var fileName = getFileName(res.headers);
	downloadByData(res.data as any, fileName);
};


// 改变页面容量
const handleSizeChange = (val: number) => {
	state.tableParams.pageSize = val;
	handleQuery();
};

// 改变页码序号
const handleCurrentChange = (val: number) => {
	state.tableParams.pageNo = val;
	handleQuery();
};

// 查看详情
const viewDetail = async (row: any) => {
	state.content = '';
	state.dialogVisible = true;
	state.loadingDetail = true;
	var res = await apiSysLogExDetailIdGet(row.id);
	row.message = res.data.data ?? '';
	state.content = row.message;
	state.loadingDetail = false;
};

// 设置行颜色
const tableRowClassName = (row: any) => {
	return row.row.exception != null ? 'warning-row' : '';
};

const shortcuts = [
	{
		text: '今天',
		value: new Date(),
	},
	{
		text: '昨天',
		value: () => {
			const date = new Date();
			date.setTime(date.getTime() - 3600 * 1000 * 24);
			return date;
		},
	},
	{
		text: '上周',
		value: () => {
			const date = new Date();
			date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
			return date;
		},
	},
];

// 列排序
const sortChange = (column: any) => {
	state.tableParams.field = column.prop;
	state.tableParams.order = column.order;
	handleQuery();
};
</script>

<style lang="scss" scoped>
.el-popper {
	max-width: 60%;
}
pre {
	white-space: break-spaces;
	line-height: 20px;
}
.el-table .warning-row {
	--el-table-tr-bg-color: var(--el-color-warning-light-9);
}
.el-table .success-row {
	--el-table-tr-bg-color: var(--el-color-success-light-9);
}
</style>
